package cn.edu.sdjzu.xg.bysj.controller.basic;

import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectType;
import cn.edu.sdjzu.xg.bysj.service.GraduateProjectTypeService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

//url映射为gpt.ctl
@WebServlet("/gpt.ctl")
public class GraduateProjectTypeController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求格式为UTF8
        req.setCharacterEncoding("UTF-8");
        //将客户端请求的信息转换成json格式字串
        String gpt_json = JSONUtil.getJSON(req);
        //将json字串解析为GraduateProjectType类对象
        GraduateProjectType toAddGraduateProjectType = JSON.parseObject(gpt_json, GraduateProjectType.class);
        //设置响应字符编码为UTF-8
        resp.setContentType("application/json;charset=UTF-8");
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        //在数据库表中增加GraduateProjectType对象
        try {
            GraduateProjectTypeService.getInstance().add(toAddGraduateProjectType);
            message.put("message", "增加成功");
        }catch (SQLException e){
            message.put("message", "数据库操作异常");
        }catch(Exception e){
            message.put("message", "网络异常");
        }
        //响应message到前端
        resp.getWriter().println(message);

    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求格式为UTF8
        req.setCharacterEncoding("UTF-8");
        //将客户端请求的信息转换成json格式字串
        String gpt_json = JSONUtil.getJSON(req);
        //将json字串解析为GraduateProjectType类对象
        GraduateProjectType toUpdateGraduateProjectType = JSON.parseObject(gpt_json, GraduateProjectType.class);
        //设置响应字符编码为UTF-8
        resp.setContentType("application/json;charset=UTF-8");
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        //在数据库表中更新GraduateProjectType对象
        try {
            GraduateProjectTypeService.getInstance().update(toUpdateGraduateProjectType);
            message.put("message","更新成功");
        }catch (SQLException e){
            message.put("message","数据库操作异常");
        }catch (Exception e){
            message.put("message","网络异常");
        }
        //响应message到前端
        resp.getWriter().println(message);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取参数id_str
        String id_str = req.getParameter("id");
        //将id_str转换成int类型的值
        int id = Integer.parseInt(id_str);
        //设置响应字符编码为UTF-8
        resp.setContentType("application/json;charset=UTF-8");
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        //在数据库表中删除GraduateProjectType对象
        try {
            GraduateProjectTypeService.getInstance().delete(id);
            message.put("message","删除成功");
        }catch (SQLException e){
            message.put("message","数据库操作异常");
        }catch (Exception e){
            message.put("message","网络异常");
        }
        //响应message到前端
        resp.getWriter().println(message);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置相应字符编码为utf8，json作为支持
        response.setContentType("application/json;charset=utf-8");
        //获得来自浏览器的请求
        String id_str = request.getParameter("id");
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        try {
            //无id请求，响应全部json
            if (id_str == null){
                this.responseGraduateProjectTypes(response);
            }else {
                //有id类型请求，响应此id的json
                int id = Integer.parseInt(id_str);
                this.responseGraduateProjectType(id , response);
            }
        }catch (SQLException e){
            message.put("message", "数据库操作异常");
            //响应message到前端
            response.getWriter().println(message);
        }catch(Exception e){
            message.put("message", "网络异常");
            //响应message到前端
            response.getWriter().println(message);
        }
    }
    //响应所有毕业设计的json
    private void responseGraduateProjectTypes (HttpServletResponse response) throws IOException,SQLException {
        //获得所有毕业设计信息
        Collection<GraduateProjectType> graduateProjectTypes = GraduateProjectTypeService.getInstance().findAll();
        //转换成json格式
        String gpts_json = JSON.toJSONString(graduateProjectTypes, SerializerFeature.DisableCircularReferenceDetect);
        //响应到浏览器
        response.getWriter().println(gpts_json);
    }
    //根据id响应对应的毕业设计的json
    private void responseGraduateProjectType (int id , HttpServletResponse response) throws IOException,SQLException {
        //根据id获得对应的毕业设计信息
        GraduateProjectType graduateProjectType = GraduateProjectTypeService.getInstance().find(id);
        //转换成json格式
        String gpt_json = JSON.toJSONString(graduateProjectType);
        //响应到浏览器
        response.getWriter().println(gpt_json);
    }
}
